
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>异常处理与 Zone · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="生成器.html" />
    
    
    <link rel="prev" href="异步编程之Isolate.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >异常处理与 Zone</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E;-zone"><b></b>&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E; Zone</a></li><ul><li><span class="title-icon "></span><a href="#&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><b></b>&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;</a></li><li><span class="title-icon "></span><a href="#&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><b></b>&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E;-zone"><a name="&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E;-zone" class="anchor-navigation-ex-anchor" href="#&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E;-zone"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0E; Zone</h2>
<h3 id="&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><a name="&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x540C;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;</h3>
<p>&#x5982;&#x679C;&#x5173;&#x5FC3;&#x5177;&#x4F53;&#x5F02;&#x5E38;&#xFF0C;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x5F02;&#x5E38;&#x8FDB;&#x884C;&#x4E0D;&#x540C;&#x5904;&#x7406;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>try...on</code>&#x5904;&#x7406;&#x5F02;&#x5E38;&#xFF0C;<code>finally</code>&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x7528;&#x4E8E;&#x6700;&#x540E;&#x7684;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">try</span> {
      <span class="hljs-comment">// &#x4F7F;&#x9664;&#x6570;&#x4E3A;0</span>
      <span class="hljs-built_in">print</span>(<span class="hljs-number">11</span>~/<span class="hljs-number">0</span>);
  } on IntegerDivisionByZeroException {
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x9664;&#x6570;&#x4E3A;0&quot;</span>);
  }on Exception{
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Exception&quot;</span>);
  }<span class="hljs-keyword">finally</span> {
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;finally&quot;</span>);
  }
</code></pre>
<p>&#x4E0D;&#x5173;&#x5FC3;&#x5177;&#x4F53;&#x5F02;&#x5E38;&#xFF0C;&#x53EA;&#x60F3;&#x6355;&#x83B7;&#xFF0C;&#x907F;&#x514D;&#x5F02;&#x5E38;&#x7EE7;&#x7EED;&#x4F20;&#x9012;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>try...catch</code>&#x5904;&#x7406;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">try</span> {
      <span class="hljs-built_in">print</span>(<span class="hljs-number">11</span>~/<span class="hljs-number">0</span>);
  } <span class="hljs-keyword">catch</span>(e){
      <span class="hljs-comment">// &#x6253;&#x5370;&#x62A5;&#x9519;&#x4FE1;&#x606F;</span>
      <span class="hljs-built_in">print</span>(e);
  }<span class="hljs-keyword">finally</span> {
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;finally&quot;</span>);
  }
</code></pre>
<p>&#x5982;&#x679C;&#x60F3;&#x83B7;&#x53D6;&#x66F4;&#x591A;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7684;<code>catch</code>&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5F02;&#x5E38;&#x7684;&#x8C03;&#x7528;&#x6808;&#x4FE1;&#x606F;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">try</span> {
      <span class="hljs-built_in">print</span>(<span class="hljs-number">11</span>~/<span class="hljs-number">0</span>);
  } <span class="hljs-keyword">catch</span>(e,s){
      <span class="hljs-built_in">print</span>(s);
  }
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x65E2;&#x60F3;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x5F02;&#x5E38;&#x8FDB;&#x884C;&#x4E0D;&#x540C;&#x5904;&#x7406;&#xFF0C;&#x8FD8;&#x60F3;&#x6253;&#x5370;&#x8C03;&#x7528;&#x6808;&#x4FE1;&#x606F;&#xFF0C;&#x90A3;&#x5C31;&#x5C06;&#x4E24;&#x79CD;&#x7ED3;&#x5408;&#x8D77;&#x6765;&#x4F7F;&#x7528;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">try</span> {
      <span class="hljs-built_in">print</span>(<span class="hljs-number">11</span>~/<span class="hljs-number">0</span>);
  } on IntegerDivisionByZeroException <span class="hljs-keyword">catch</span>(e,s){
      <span class="hljs-built_in">print</span>(s);
  } on Exception <span class="hljs-keyword">catch</span>(e,s){
      <span class="hljs-built_in">print</span>(s);
  }
</code></pre>
<h3 id="&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><a name="&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x7684;&#x5904;&#x7406;</h3>
<p>&#x5728;Dart&#x4E2D;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x5F02;&#x5E38;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x8C03;&#x7528;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#x65F6;&#x4F7F;&#x7528;<code>await</code>&#xFF0C;&#x7136;&#x540E;&#x518D;&#x4F7F;&#x7528;<code>try/catch</code>&#x6355;&#x83B7;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x662F;&#x4F7F;&#x7528;Future &#x7684; <code>API</code>&#xFF0C;&#x5373;&#x901A;&#x8FC7;<code>catchError</code>&#x6765;&#x6355;&#x83B7;&#x3002;&#x6700;&#x540E;&#x4E00;&#x79CD;&#x5219;&#x662F;&#x4F7F;&#x7528;<code>Zone</code>&#x3002;</p>
<h4 id="&#x4EC0;&#x4E48;&#x662F;zone&#xFF1F;"><a name="&#x4EC0;&#x4E48;&#x662F;zone&#xFF1F;" class="anchor-navigation-ex-anchor" href="#&#x4EC0;&#x4E48;&#x662F;zone&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4EC0;&#x4E48;&#x662F;Zone&#xFF1F;</h4>
<p>&#x5B83;&#x57FA;&#x672C;&#x4E0A;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7684;&#x6267;&#x884C;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x5728;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x548C;&#x5206;&#x6790;&#x65F6;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;&#x66F4;&#x7B80;&#x5355;&#x8BF4;&#xFF0C;&#x5B83;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6C99;&#x7BB1;&#x73AF;&#x5883;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x51FD;&#x6570;&#x5728;&#x8FD9;&#x4E2A;&#x6C99;&#x7BB1;&#x73AF;&#x5883;&#x4E2D;&#x6267;&#x884C;&#x3002;</p>
<p>Dart&#x4E2D;&#x6709;&#x4E00;&#x4E2A;<code>runZoned()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x7ED9;&#x6267;&#x884C;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;Zone&#x3002;&#x4E0D;&#x540C;Zone&#x4E4B;&#x95F4;&#x662F;&#x9694;&#x79BB;&#x7684;&#xFF0C;&#x8FD9;&#x4E9B;&#x6C99;&#x7BB1;&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x3001;&#x62E6;&#x622A;&#x6216;&#x4FEE;&#x6539;&#x4E00;&#x4E9B;&#x4EE3;&#x7801;&#x884C;&#x4E3A;&#xFF0C;&#x5982;Zone&#x4E2D;&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#x3001;Timer&#x521B;&#x5EFA;&#x3001;&#x5FAE;&#x4EFB;&#x52A1;&#x8C03;&#x5EA6;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x540C;&#x65F6;Zone&#x4E5F;&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x6240;&#x6709;&#x672A;&#x5904;&#x7406;&#x7684;&#x5F02;&#x5E38;&#x3002;</p>
<h4 id="&#x7B80;&#x5355;&#x793A;&#x4F8B;"><a name="&#x7B80;&#x5355;&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x7B80;&#x5355;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7B80;&#x5355;&#x793A;&#x4F8B;</h4>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
   runZoned((){
     Future(() { <span class="hljs-keyword">throw</span> <span class="hljs-string">&quot;asynchronous error&quot;</span>; });
   },onError: (<span class="hljs-built_in">Object</span> o, StackTrace st){
     <span class="hljs-built_in">print</span>(o);
     <span class="hljs-built_in">print</span>(st);
   });
}
</code></pre>
<h4 id="zone&#x7684;&#x4F7F;&#x7528;"><a name="zone&#x7684;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#zone&#x7684;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>Zone&#x7684;&#x4F7F;&#x7528;</h4>
<p>&#x5728;Dart  &#x5168;&#x5C40;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;<code>root zone</code>&#xFF0C;<code>main</code>&#x51FD;&#x6570;&#x5C31;&#x8FD0;&#x884C;&#x5728;&#x8FD9;&#x4E2A;<code>zone</code>&#x4E2D;&#x3002;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;Dart &#x7684;&#x6240;&#x6709;&#x4EE3;&#x7801;&#x90FD;&#x8FD0;&#x884C;&#x5728;&#x4E00;&#x4E2A;&#x53EB;&#x505A;<code>root zone</code>&#x7684;&#x6C99;&#x7BB1;&#x4E2D;&#xFF0C;&#x5305;&#x62EC;&#x6240;&#x6709;<code>isolate</code>&#x4E2D;&#x7684;<code>main</code>&#x51FD;&#x6570;&#x548C;spawned &#x51FD;&#x6570;&#x3002;</p>
<blockquote>
<p> All isolate entry functions (<code>main</code> or spawned functions) start running in the root zone (that is, [Zone.current] is identical to [Zone.root] when the entry function is called).</p>
</blockquote>
<pre><code class="lang-dart">  <span class="hljs-comment">// &#x5C06;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x653E;&#x5728;root zone&#x4E2D;&#x6267;&#x884C;</span>
  Zone.root.run((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;root run&quot;</span>);
  });

  <span class="hljs-comment">// &#x4ECE;&#x5F53;&#x524D;&#x7684;zone&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;zone</span>
  Zone myZone = Zone.current.fork();
  myZone.run((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;myZone run&quot;</span>);
  });
</code></pre>
<p>&#x4EC5;&#x4EC5;&#x5728;<code>root zone</code>&#x4E2D;&#x8FD0;&#x884C;&#x51FD;&#x6570;&#x4F3C;&#x4E4E;&#x610F;&#x4E49;&#x4E0D;&#x5927;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x505A;&#x4E00;&#x4E9B;&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>fork</code>&#x51FD;&#x6570;&#x4ECE;&#x5DF2;&#x6709;&#x7684;<code>zone</code>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B50;<code>zone</code>&#xFF0C;&#x8FD9;&#x6709;&#x4E9B;&#x7C7B;&#x4F3C;&#x4E8E;&#x4ECE;&#x7236;<code>zone</code>&#x7EE7;&#x627F;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// fork&#x51FD;&#x6570;&#x539F;&#x578B;</span>
Zone fork({ZoneSpecification specification, <span class="hljs-built_in">Map</span> zoneValues});
</code></pre>
<ul>
<li><code>specification</code>&#xFF1A;zone&#x7684;&#x4E00;&#x4E9B;&#x914D;&#x7F6E;&#xFF0C;&#x7528;&#x4E8E;&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x884C;&#x4E3A;&#xFF0C;&#x6BD4;&#x5982;&#x62E6;&#x622A;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#x3001;&#x6CE8;&#x518C;&#x56DE;&#x8C03;&#x7B49;&#x7B49;</li>
<li><code>zoneValues</code> zone &#x7684;&#x79C1;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x901A;&#x8FC7;&#x5B9E;&#x4F8B;<code>zone[key]</code>&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#xFF0C;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x88AB;&#x5B50;zone&#x7EE7;&#x627F;&#x3002;</li>
</ul>
<pre><code class="lang-dart">  Zone myZone = Zone.current.fork(zoneValues: {<span class="hljs-string">&quot;a&quot;</span>:<span class="hljs-number">1</span>});
  myZone.run(()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;myZone run&quot;</span>));
  <span class="hljs-comment">// &#x5728;&#x5B50;Zone&#x4E2D;&#x8BBF;&#x95EE;&#x7236;Zone&#x4E2D;&#x7684;&#x6570;&#x636E;</span>
  Zone childZone = myZone.fork();
  childZone.run(()=&gt;<span class="hljs-built_in">print</span>(childZone[<span class="hljs-string">&quot;a&quot;</span>]));
</code></pre>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  Zone myZone = Zone.current.fork(
    specification:ZoneSpecification(
      <span class="hljs-comment">// &#x5904;&#x7406;&#x56DE;&#x8C03;</span>
      registerCallback: &lt;R&gt;(self, parent, zone, f) {
        f();
        <span class="hljs-keyword">return</span> f;
      },

      <span class="hljs-comment">// &#x5904;&#x7406;run&#x51FD;&#x6570;&#x4E2D;&#x6267;&#x884C;&#x7684;&#x4EFB;&#x52A1;</span>
      run:&lt;R&gt;(self, parent, zone, f) {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Enter the run method&quot;</span>);
        <span class="hljs-keyword">return</span> parent.run(zone, f); 
      }));

  <span class="hljs-comment">// &#x5728;zone&#x4E2D;&#x6267;&#x884C;&#x4EFB;&#x52A1;</span>
  myZone.run((){
    <span class="hljs-comment">// &#x6CE8;&#x518C;&#x56DE;&#x8C03;</span>
    Zone.current.registerCallback(()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;callback start&quot;</span>));
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;myZone run task...&quot;</span>);
    Zone.current.registerCallback(()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;callback end&quot;</span>));
  });
}
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>Enter the run method
callback start
myZone run task...
callback end
</code></pre><p>&#x62E6;&#x622A;&#x5E76;&#x4FEE;&#x6539;<code>print</code>&#x51FD;&#x6570;&#x7684;&#x884C;&#x4E3A;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
    Zone myZone = Zone.current.fork(
    specification:ZoneSpecification(
      <span class="hljs-comment">//&#x62E6;&#x622A;&#x5E76;&#x4FEE;&#x6539; print &#x51FD;&#x6570;&#x7684;&#x8F93;&#x51FA;</span>
      <span class="hljs-built_in">print</span>: (Zone self, ZoneDelegate parent, Zone zone, <span class="hljs-built_in">String</span> line) {
        parent.<span class="hljs-built_in">print</span>(zone, <span class="hljs-string">&quot;test info: $line&quot;</span>);
      },

      <span class="hljs-comment">// &#x5904;&#x7406;&#x672A;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x5E38;</span>
      handleUncaughtError: (Zone self,ZoneDelegate parent, Zone zone, <span class="hljs-built_in">Object</span> error, StackTrace stackTrace){
        <span class="hljs-built_in">print</span>(error);
    }));

  myZone.run((){
    <span class="hljs-comment">// &#x6253;&#x5370;&#x4E00;&#x53E5;&#x8BDD;</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;myZone run task...&quot;</span>);
    <span class="hljs-comment">// &#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x9519;&#x8BEF;</span>
    Future.error(<span class="hljs-string">&quot;generate error&quot;</span>);
  });
}
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>test info: myZone run task...
test info: generate error
</code></pre><p>&#x5728;Dart&#x4E2D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>runZoned</code>&#x51FD;&#x6570;&#x7B80;&#x5316;&#x7F16;&#x5199;&#xFF0C;&#x5176;&#x539F;&#x578B;&#x5982;&#x4E0B;</p>
<pre><code class="lang-dart">R runZoned&lt;R&gt;(R body(),
    {<span class="hljs-built_in">Map</span> zoneValues, ZoneSpecification zoneSpecification, <span class="hljs-built_in">Function</span> onError})
</code></pre>
<p>&#x7B80;&#x5316;&#x793A;&#x4F8B;</p>
<pre><code class="lang-dart">  runZoned((){
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;myZone run task...&quot;</span>);
      Future.error(<span class="hljs-string">&quot;generate error&quot;</span>);
    },
    zoneSpecification: ZoneSpecification(
      <span class="hljs-built_in">print</span>: (Zone self, ZoneDelegate parent, Zone zone, <span class="hljs-built_in">String</span> line) {
        parent.<span class="hljs-built_in">print</span>(zone, <span class="hljs-string">&quot;test info: $line&quot;</span>);
      },

      handleUncaughtError: (Zone self,ZoneDelegate parent, Zone zone, <span class="hljs-built_in">Object</span> error, StackTrace stackTrace){
        <span class="hljs-built_in">print</span>(error);
    }));
</code></pre>
<h4 id="&#x5173;&#x4E8E;&#x62E6;&#x622A;&#x5668;&#x4E2D;&#x7684;&#x53C2;&#x6570;"><a name="&#x5173;&#x4E8E;&#x62E6;&#x622A;&#x5668;&#x4E2D;&#x7684;&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x5173;&#x4E8E;&#x62E6;&#x622A;&#x5668;&#x4E2D;&#x7684;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5173;&#x4E8E;&#x62E6;&#x622A;&#x5668;&#x4E2D;&#x7684;&#x53C2;&#x6570;</h4>
<ul>
<li><p><code>self</code>
&#x5904;&#x7406;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;Zone</p>
</li>
<li><p><code>parent</code>
&#x8BE5;&#x59D4;&#x6258;&#x8868;&#x793A;&#x7236;Zone&#xFF0C;&#x901A;&#x8FC7;&#x5B83;&#x5C06;&#x64CD;&#x4F5C;&#x8F6C;&#x53D1;&#x7ED9;&#x7236;Zone</p>
</li>
<li><p><code>zone</code>
&#x8868;&#x793A;&#x6267;&#x884C;<code>run</code>&#x64CD;&#x4F5C;&#x7684; zone&#x3002;&#x5F88;&#x591A;&#x64CD;&#x4F5C;&#x9700;&#x8981;&#x660E;&#x786E;&#x8BE5;&#x64CD;&#x4F5C;&#x662F;&#x5728;&#x54EA;&#x4E2A;Zone&#x4E2D;&#x88AB;&#x8C03;&#x7528;</p>
</li>
</ul>
<h4 id="&#x603B;&#x7ED3;"><a name="&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;</h4>
<p>Dart&#x4E2D;&#x7684;Zone&#x9664;&#x4E86;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x5F02;&#x5E38;&#xFF0C;&#x66F4;&#x91CD;&#x8981;&#x7684;&#x662F;&#x5B83;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;Hook&#x673A;&#x5236;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x5BF9;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x6027;&#x80FD;&#x5206;&#x6790;&#x7B49;&#x3002;</p>
<p>&#x4E3B;&#x8981;&#x63D0;&#x4F9B;&#x4E86;&#x4E0B;&#x5217;&#x529F;&#x80FD;</p>
<ul>
<li>Fork&#x5B50;&#x5206;&#x533A;&#x5904;&#x7406;</li>
<li>&#x5728;&#x5206;&#x533A;&#x4E2D;&#x6CE8;&#x518C;&#x548C;&#x8FD0;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
<li>&#x8C03;&#x5EA6;Microtask&#x5FAE;&#x4EFB;&#x52A1;&#x548C;Timer&#x5B9A;&#x65F6;&#x5668;</li>
<li>&#x5904;&#x7406;&#x672A;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x6B65;&#x7C7B;&#x578B;&#x7684;&#x5F02;&#x5E38;</li>
<li>Print &#x8F93;&#x51FA;&#x5904;&#x7406;</li>
</ul>
<p>&#x67E5;&#x770B;<code>ZoneSpecification</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5373;&#x53EF;&#x77E5;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">const</span> <span class="hljs-keyword">factory</span> ZoneSpecification(
      {HandleUncaughtErrorHandler handleUncaughtError,
      RunHandler run,
      RunUnaryHandler runUnary,
      RunBinaryHandler runBinary,
      RegisterCallbackHandler registerCallback,
      RegisterUnaryCallbackHandler registerUnaryCallback,
      RegisterBinaryCallbackHandler registerBinaryCallback,
      ErrorCallbackHandler errorCallback,
      ScheduleMicrotaskHandler scheduleMicrotask,
      CreateTimerHandler createTimer,
      CreatePeriodicTimerHandler createPeriodicTimer,
      PrintHandler <span class="hljs-built_in">print</span>,
      ForkHandler fork})
</code></pre>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:34:49
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="异步编程之Isolate.html" class="navigation navigation-prev " aria-label="Previous page: Isolate">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="生成器.html" class="navigation navigation-next " aria-label="Next page: 生成器">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"异常处理与 Zone","level":"1.3.6","depth":2,"next":{"title":"生成器","level":"1.3.7","depth":2,"path":"生成器.md","ref":"生成器.md","articles":[]},"previous":{"title":"Isolate","level":"1.3.5.3","depth":3,"path":"异步编程之Isolate.md","ref":"异步编程之Isolate.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"异常处理.md","mtime":"2021-03-18T05:34:49.256Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

